package com.jlastu.znb.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.jlastu.znb.R;

public class WenViewActivity extends AppCompatActivity {
    private WebView webView;
    private TextView tvTitle;
    private ImageView imgBack;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        String url =getIntent().getStringExtra("url");
        webView = findViewById(R.id.webview);
        tvTitle =  findViewById(R.id.tv_title);
        imgBack =  findViewById(R.id.img_back);
        webView.loadUrl(url);// 修改成要封装的网址
        //webView的相关设置
        imgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        WebSettings websettings = webView.getSettings();

        websettings.setJavaScriptEnabled(true); // 允许执行JavaScript代码的语句。
        websettings.setBlockNetworkImage(false); //允许加载网络图片的
        websettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        websettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);    //开启H5(APPCache)缓存功能
        websettings.setSupportZoom(true); //允许用户在WebView中放大和缩小网页内容。
        websettings.setCacheMode(WebSettings.LOAD_DEFAULT);//使用系统默认的缓存策略。
        websettings.setAllowFileAccess(true);    // 可以读取文件缓存(manifest生效)
        websettings.setDatabaseEnabled(true);    //允许使用Web Storage API（如localStorage或sessionStorage）或IndexedDB来存储数据。
        websettings.setDomStorageEnabled(true);  // 开启 DOM storage 功能
        websettings.setDisplayZoomControls(true); //是否显示缩放控件
        websettings.setDefaultTextEncodingName("utf-8");//文字编码utf-8
        websettings.setRenderPriority(WebSettings.RenderPriority.HIGH); //提高渲染的优先级
        websettings.setMediaPlaybackRequiresUserGesture(true);// 不会自动播放视频和音频
        websettings.setJavaScriptCanOpenWindowsAutomatically(false);//禁止JavaScript自动打开新窗口
        webView.setWebViewClient(new WebViewClient());
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                    tvTitle.setText(TextUtils.isEmpty(title)?"标题头":title);
            }
        });
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.canGoBack()) {
                webView.goBack();
                return true;


            }
        }
        return super.onKeyDown(keyCode, event);

    }
}
